
package esoft2.ipleiria.mybio;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import esoft2.ipleiria.mybio.model.Contacto;


public class MainActivity extends Activity {

    public static final String ISCHECKEDIN = "esoft2.ipleiria.mybio.ISCHECKEDIN";
    private boolean isCheckedIn = false;
    private Contacto contacto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.contacto = new Contacto("Zé", "ze@email.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        MenuItem id = menu.findItem(R.id.action_checkin);

        if (id != null) {
            updateMenuItemCheckedIn(id);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_checkin) {


            if (contacto != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Confirmação");
                builder.setMessage("Tem a certeza de que deseja partilhar os seus dados?");
                // Add the buttons
                builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        if (isOnline()) {
                            isCheckedIn = !isCheckedIn;

                            if (isCheckedIn) {

                                Intent intent = new Intent(getApplicationContext(), ParticipantsActivity.class);
                                intent.putExtra("esoft2.ipleiria.mybio.ISCHECKEDIN", isCheckedIn);
                                startActivity(intent);
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "Não está online", Toast.LENGTH_LONG).show();
                        }
                    }
                });
                builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(MainActivity.this, "Cancelado", Toast.LENGTH_LONG).show();

                    }
                });
                // Set other dialog properties
                //...

                // Create the AlertDialog
                AlertDialog dialog = builder.create();
                dialog.show();

                updateMenuItemCheckedIn(item);
            } else {
                Toast.makeText(this, "Contacto inexistente", Toast.LENGTH_LONG).show();
            }


        }

        return super.onOptionsItemSelected(item);
    }

    private void updateMenuItemCheckedIn(MenuItem menuItemCheckedIn) {
        menuItemCheckedIn.setIcon(isCheckedIn ? R.drawable.ic_action_group : R.drawable.ic_action_person);
    }

    private boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }
}
